package com.example.service;

import com.example.mapper.ClickLikeMapper;
import com.example.mapper.JobSeekerMapper;
import com.example.pojo.dao.CommentRepository;
import com.example.pojo.dao.MongoIdRepository;
import com.example.pojo.entity.Comment;
import com.example.pojo.entity.JobSeeker;
import com.example.pojo.entity.MongoId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/21
 */
@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    @Autowired
    private MongoIdRepository mongoIdRepository;
    @Autowired
    private ClickLikeMapper clickLikeMapper;

    @Override
    public List<Comment> findByProblemId(String problemId) {
        List<Comment> list = commentRepository.findAllByProblemIdOrderByLikeNum(problemId);
        return list;
    }

    @Override
    public int saveComment(Integer jsId, String problemId, String comment) {
        JobSeeker jobSeeker = jobSeekerMapper.findJSById(jsId);
        Comment cm = new Comment();
        cm.setComment(comment);
        cm.setCreateTime(new Date());
        cm.setAvatar(jobSeeker.getAvatar());
        cm.setJsName(jobSeeker.getJsName());
        cm.setJsId(jsId);
        cm.setLikeNum(0);
        cm.setProblemId(problemId);
        commentRepository.save(cm);
        MongoId mongoId = mongoIdRepository.findById("6532b1421144d177747c8610").orElse(null);
        if (mongoId!=null){
            mongoId.setCommentId(mongoId.getCommentId()+1);
            mongoIdRepository.save(mongoId);
        }
        return 1;
    }

    @Override
    public int addCommentLikeNum(Integer jsId, String commentId) {
        Comment comment = commentRepository.findById(commentId).orElse(null);
        if (comment!=null){
            comment.setLikeNum(comment.getLikeNum()+1);
            commentRepository.save(comment);
            clickLikeMapper.save(jsId,commentId,1);
        }
        return 1;
    }


}
